﻿using UnityEngine;
using System.Collections;

namespace UI
{
    public class UIFieldRunMapEvent : MonoBehaviour
    {
        /// <summary>
        /// 退出
        /// </summary>
        public void Exit()
        {
            if (!CanOpt()) return;
            var room = Room.RoomManager.currentRoom as Room.RoomField;
            var self = room.charactorManager.GetSelfCharactor();
            if (self != null)
            {
                var dict = Dict.Blo.DictFieldBlo.GetOne(StaticData.fieldId).Resurrection;
                var sss = dict.Split(new char[1] { ',' }, System.StringSplitOptions.RemoveEmptyEntries);
                var pos = new Vector3(float.Parse(sss[0]), float.Parse(sss[1]), float.Parse(sss[2]));
                var ai = self.ai as Room.RCAISelf;
                ai.AutoGoto(pos, () =>
                 {
                     //GetComponent<UIFieldRunMap>().Close();
                     PageManager.instance.OpenPage("WorldMapPage", "");
                     UIAutoSize.Instance.ShowMaskUI(true);
                 });
            }
        }
        
        /// <summary>
        /// 说明
        /// </summary>
        public void Explain()
        {
            if (!CanOpt()) return;
            PageManager.instance.OpenPage("DescriptionPage", "");
            var page = UI.PageManager.Instance.currentPage as UI.DescriptionPage;
            if (page != null)
            {

                page.SetText("field_des");
                page.SetStyle(DescriptionPage.DesType.Field);
            }
        }

        /// <summary>
        /// 战斗记录
        /// </summary>
        public void CombatLog()
        {
            if (!CanOpt()) return;


            GetComponent<UIFieldRunMap>().combatLog.Init();

        }

        public void ChangeLine()
        {
            if (!CanOpt()) return;

            var room = Room.RoomManager.currentRoom as Room.RoomField;
            var self = room.charactorManager.GetSelfCharactor();
            if (self != null)
            {
                var dict = Dict.Blo.DictFieldBlo.GetOne(StaticData.fieldId).Resurrection;
                var sss = dict.Split(new char[1] { ',' }, System.StringSplitOptions.RemoveEmptyEntries);
                var pos = new Vector3(float.Parse(sss[0]), float.Parse(sss[1]), float.Parse(sss[2]));
                var ai = self.ai as Room.RCAISelf;
                ai.AutoGoto(pos, () =>
                {
                    GetComponent<UIFieldRunMap>().changleLine.Init();
                });
            }
            
        }

       

        /// <summary>
        /// 换阵
        /// </summary>
        public void SwitchCamp()
        {
            if (!CanOpt()) return;
            PageManager.Instance.OpenPage("FormationPopupPage", "");
        }
        [HideInInspector]
        public GameObject skipFlag;
        public void SkipBattle()
        {
            skipFlag.SetActive(!skipFlag.activeSelf);
        }


        public bool CanOpt()
        {
            var ai = Room.RoomManager.currentRoom.charactorManager.GetSelfCharactor().ai as Room.RCAIActor;
            return ai.curState == Room.PlayerAIState.Normal;
        }

        //设置血药
        public UIFieldHpSetPanel hpSetPanel;
        public void OnClickSetHpBtn()
        {
            if (!CanOpt()) return;
            var page = PageManager.Instance.currentPage as UIFieldRunMap;
            if (page != null && page.hpSetPage != null)
            {
                page.hpSetPage.show(hpSetPanel);
            }
        }

        public void OnClickLeftHpBtn()
        {
            if (!CanOpt()) return;
            hpSetPanel.GetComponent<UIFieldHpSetPanel>().onClickLeft();

        }

        public void OnClickRightHpBtn()
        {
            if (!CanOpt()) return;
            hpSetPanel.GetComponent<UIFieldHpSetPanel>().onClickRight();
        }

        /// <summary>
        /// 点击队伍信息
        /// </summary>
        public void OnOpenTeamPanelInfo()
        {
            if (!CanOpt()) return;
            GetComponent<UIFieldRunMap>().teamPanel.Init();
        }
    }
}